import dotEnv from 'dotenv'
import * as util from './util'

export interface MetaData {
    pipelineId: string | undefined

    buildNumber: string | undefined

    pipelineName: string | undefined

    executorId: string | undefined

    executorName: string | undefined

    sources: {} | undefined

    buildJobId: string | undefined

    sourceValue: string | undefined

    ciCommitId: string | undefined

    ciCommitSha: string | undefined

    ciCommitRefName: string | undefined

    ciCommitTitle: string | undefined
}

export function getMetaData(): MetaData {
    //加载环境变量
    let envPath = process.env['WORK_SPACE'] + util.SEP + 'stepContext'
    dotEnv.config({path: envPath})

    let metaData = {} as MetaData
    metaData.buildJobId = process.env['BUILD_JOB_ID']
    metaData.buildNumber = process.env['BUILD_NUMBER']
    metaData.pipelineId = process.env['PIPELINE_ID']
    metaData.pipelineName = process.env['PIPELINE_NAME']
    metaData.executorId = process.env['EXECUTOR_ID']
    metaData.executorName = process.env['EXECUTOR_NAME']
    metaData.sourceValue = process.env['source']
    metaData.ciCommitId = process.env['CI_COMMIT_ID']
    metaData.ciCommitRefName = process.env['CI_COMMIT_REF_NAME']
    metaData.ciCommitSha = process.env['CI_COMMIT_SHA']
    metaData.ciCommitTitle = process.env['CI_COMMIT_TITLE']

    return metaData
}
